package com.example.demotest.config;

import com.example.demotest.pojo.ProductCategory;
import com.example.demotest.repository.ProductCategoryRepository;
import jakarta.transaction.Transactional;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DataInitializer implements CommandLineRunner {
    private final ProductCategoryRepository categoryRepository;

    public DataInitializer(ProductCategoryRepository categoryRepository) {
        this.categoryRepository = categoryRepository;
    }

    @Override
    @Transactional
    public void run(String... args) throws Exception {
        if (categoryRepository.count() == 0) { // 避免重复插入
            ProductCategory electronics = new ProductCategory();
            electronics.setName("电子产品");

            ProductCategory clothing = new ProductCategory();
            clothing.setName("服装");

            ProductCategory books = new ProductCategory();
            books.setName("图书");

            ProductCategory homeAppliances = new ProductCategory();
            homeAppliances.setName("家用电器");

            ProductCategory sports = new ProductCategory();
            sports.setName("运动户外");

            ProductCategory furniture = new ProductCategory();
            furniture.setName("家具");

            ProductCategory toys = new ProductCategory();
            toys.setName("玩具");

            ProductCategory beauty = new ProductCategory();
            beauty.setName("美妆护肤");

            ProductCategory automotive = new ProductCategory();
            automotive.setName("汽车用品");

            ProductCategory digitalAccessories = new ProductCategory();
            digitalAccessories.setName("数码配件");

            ProductCategory babyProducts = new ProductCategory();
            babyProducts.setName("母婴用品");

            ProductCategory musicalInstruments = new ProductCategory();
            musicalInstruments.setName("乐器");

            categoryRepository.saveAll(List.of(
                    electronics, clothing, books, homeAppliances, sports, furniture,
                    toys, beauty, automotive, digitalAccessories, babyProducts, musicalInstruments
            ));

            System.out.println("初始化分类数据完成");
        }
    }
}
